package world.snowcrystal.nova.model.vo;

import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import world.snowcrystal.nova.model.dto.question.QuestionDto;
import world.snowcrystal.nova.model.entity.Question;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 题目视图
 *

 */
@Data
@Accessors(chain = true)
@NoArgsConstructor
public class QuestionVO implements Serializable {

    /**
     * id
     */
    private Long id;


    private List<QuestionDto> questionContent;


    private Long appId;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 标签列表
     */
    private List<String> tagList;

    /**
     * 创建用户信息
     */
    private UserVO user;

    /**
     * 封装类转对象
     *
     * @param questionVO
     * @return
     */
    public static Question voToObj(QuestionVO questionVO) {
        if (questionVO == null) {
            return null;
        }
        Question question = new Question();
        BeanUtils.copyProperties(questionVO, question);

        question.setQuestionContent(JSONUtil.toJsonStr(questionVO.getQuestionContent()));
        return question;
    }

    /**
     * 对象转封装类
     *
     * @param question
     * @return
     */
    public static QuestionVO objToVo(Question question) {
        if (question == null) {
            return null;
        }
        QuestionVO vo = new QuestionVO();
        BeanUtils.copyProperties(question, vo);
        if(StringUtils.isNotBlank(question.getQuestionContent())){
            vo.setQuestionContent(JSONUtil.toList(question.getQuestionContent(), QuestionDto.class));
        }
        return vo;
    }
}
